package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.entity.Warehouse;
import com.sevattal.service.leaves.entity.vo.WarehouseVo;
import com.sevattal.service.leaves.mapper.WarehouseMapper;
import com.sevattal.service.leaves.service.WarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 仓库表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {

    @Override
    public boolean createWarehouse(Warehouse warehouse) {
        if (warehouse.getFWhid() == null || StringUtils.isEmpty(warehouse.getFWhid())
        || warehouse.getFWhname() == null || StringUtils.isEmpty(warehouse.getFWhname())){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        QueryWrapper<Warehouse> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("f_whId",warehouse.getFWhid()).or().eq("f_whName",warehouse.getFWhname());
        int count = baseMapper.selectCount(queryWrapper);
        if (count > 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        int result = baseMapper.insert(warehouse);
        return result == 1;
    }

    @Override
    public boolean updateWarehouse(Warehouse warehouse) {
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_whId",warehouse.getFWhid());
        int count = baseMapper.selectCount(queryWrapper);
        if (count == 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        int result = baseMapper.update(warehouse,queryWrapper);
        return result == 1;
    }

    @Override
    public boolean deleteWarehouse(String id) {
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_whId", id);
        int count = baseMapper.delete(queryWrapper);
        return count == 1;
    }

    @Override
    public List<WarehouseVo> getWarehouseList(WarehouseVo warehouseVo) {
        QueryWrapper<WarehouseVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("wh.f_whId");
        queryWrapper.eq("wh.is_disabled",0);
        queryWrapper.eq("wh.is_deleted",0);
        if (warehouseVo.getFWhid() != null && !StringUtils.isEmpty(warehouseVo.getFWhid())){
            queryWrapper.eq("wh.f_whId",warehouseVo.getFWhid());
        }
        if (warehouseVo.getFWhname() != null && !StringUtils.isEmpty(warehouseVo.getFWhname())){
            queryWrapper.eq("wh.f_whName",warehouseVo.getFWhname());
        }
        List<WarehouseVo> warehouseVoList = baseMapper.getWarehouseList(queryWrapper);
        return warehouseVoList;
    }

    @Override
    public WarehouseVo getWarehouse(String id) {
        return  baseMapper.getWarehouse(id);
    }
}
